<?php
namespace App\Api\Controllers\Ccb;

use App\Api\Controllers\Cmbc\PayController;
use App\Api\Controllers\Config\CcbConfigController;
use App\Api\Controllers\Config\CmbcPayConfigController;
use App\Api\Controllers\lianfuyouzheng\BaseController;
use App\Common\PaySuccessAction;
use App\Common\RefundSuccessAction;
use App\Models\MerchantWalletDetail;
use App\Models\RefundOrder;
use App\Models\UserWalletDetail;
use Illuminate\Support\Facades\Cache;

class LogicController extends BaseController
{
    public function __construct()
    {

    }

    public function payParams ($data_insert, $data)
    {
        try {
            //读取配置
            $store_id = $data['store_id'];
            $store_pid = $data['store_pid'];
            $merchant_id = $data['merchant_id'];
            $total_amount = $data['total_amount'];
            $ways_type = $data['ways_type'];
            $store_name = $data['store_name'];
            $config_id = $data['config_id'];
            $tg_user_id = $data['tg_user_id'];
            $code=$data['code'];
            $data['other_no'] = isset($data['other_no']) ? $data['other_no'] : "";
            $config = new CcbConfigController();
            $h_merchant = $config->ccb_merchant($store_id, $store_pid);
            if (!$h_merchant) {
                return json_encode([
                    'status' => 2,
                    'message' => '建设银行商户号不存在'
                ]);
            }

            $out_trade_no = $data['out_trade_no'];

            //入库参数
            $data_insert['out_trade_no'] = $out_trade_no;
            $data_insert['ways_type'] = $data['ways_type'];
            $data_insert['ways_type_desc'] = $data['ways_type_desc'];
            $data_insert['ways_source'] = $data['ways_source'];
            $data_insert['company'] = $data['company'];
            $data_insert['ways_source_desc'] = $data['ways_source_desc'];
            $data_insert['other_no'] = $data['other_no'];

            $insert_re = $this->insert_day_order($data_insert);
            if (!$insert_re) {
                return json_encode([
                    'status' => 2,
                    'message' => '订单未入库'
                ]);
            }

            $obj = new \App\Api\Controllers\lianfu\PayController();
            $data1['MERCHANTID'] = $h_merchant->merchantid;//商户号
            $data1['POSID'] = $h_merchant->posid;//柜台号
            $data1['BRANCHID'] = $h_merchant->bankchid;//分行号
            $data1['TXCODE'] = 'PAY100';//交易码
            $data1['MERFLAG'] = 1;//商户类型
            $data1['ORDERID'] = $out_trade_no;//商户订单号
            $data1['AMOUNT'] = $total_amount;//交易金额
            $data1['QRCODE'] = $code;//条码信息
            $return = $obj->scan_pay_ccb($data1,$h_merchant->pubkey);

            if ($return['status'] == 0) {
                return json_encode([
                    'status' => 2,
                    'message' => $return['message']
                ]);
            }

            //返回支付成功
            if ($return['status'] == 1) {
                $trade_no = $return['data']['ORDERID'];
                $user_info = '';
                $pay_time = date('Y-m-d H:i:s', time());
                $buyer_pay_amount = $total_amount;
                $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');

                $update_data = [
                    'trade_no' => $trade_no,
                    'buyer_id' => $user_info,
                    'buyer_logon_id' => '',
                    'status' => '1',
                    'pay_status_desc' => '支付成功',
                    'pay_status' => 1,
                    'payment_method' => '',
                    'pay_time' => $pay_time,
                    'buyer_pay_amount' => $buyer_pay_amount, //用户实际支付
                ];
                $this->update_day_order($update_data, $out_trade_no);

                //支付成功后的动作
                $data = [
                    'ways_type' => $data['ways_type'],
                    'ways_type_desc' => $data['ways_type_desc'],
                    'company' => $data_insert['company'],
                    'source_type' => '30050', //返佣来源
                    'source_desc' => 'wing', //返佣来源说明
                    'total_amount' => $total_amount,
                    'other_no' => $data['other_no'],
                    'out_trade_no' => $out_trade_no,
                    'rate' => $data_insert['rate'],
                    'fee_amount' => $data_insert['fee_amount'],
                    'merchant_id' => $merchant_id,
                    'store_id' => $store_id,
                    'user_id' => $tg_user_id,
                    'config_id' => $config_id,
                    'store_name' => $store_name,
                    'ways_source' => $data['ways_source'],
                    'pay_time' => $pay_time,
                    'device_id' => isset($data['device_id']) ? $data['device_id'] : "",
                ];
                PaySuccessAction::action($data);

                return json_encode([
                    'status' => 1,
                    'pay_status' => '1',
                    'message' => '支付成功',
                    'data' => [
                        'out_trade_no' => $out_trade_no,
                        'ways_type' => $ways_type,
                        'total_amount' => $total_amount,
                        'store_id' => $store_id,
                        'store_name' => $store_name,
                        'config_id' => $config_id,
                        'pay_time' => $pay_time,
                        'trade_no' => $trade_no,
                        'ways_source' => $data['ways_source'],
                    ]
                ]);
            }

            //正在支付
            if ($return['status'] == 2) {
                return json_encode([
                    'status' => 1,
                    'pay_status' => '2',
                    'message' => '正在支付',
                    'data' => [
                        'out_trade_no' => $out_trade_no,
                        'ways_type' => $ways_type,
                        'ways_source' => $data['ways_source'],
                        'total_amount' => $total_amount,
                        'store_id' => $store_id,
                        'store_name' => $store_name,
                        'config_id' => $config_id,
                    ]
                ]);
            }

            //支付失败
            if ($return['status'] == 3) {
                return json_encode([
                    'status' => 2,
                    'pay_status' => '3',
                    'message' => '支付失败',
                    'data' => [
                        'out_trade_no' => $out_trade_no,
                        'ways_type' => $ways_type,
                        'ways_source' => $data['ways_source'],
                        'total_amount' => $total_amount,
                        'store_id' => $store_id,
                        'store_name' => $store_name,
                        'config_id' => $config_id,
                    ]
                ]);
            }
        } catch (\Exception $e) {
            return json_encode([
                'status' => -1,
                'message' => $e->getMessage().' | '.$e->getFile().' | '.$e->getLine()
            ]);
        }
    }

    public function queryParams ($store_id, $store_pid, $data, $order)
    {
        try {
            $config_id = $data['config_id'] ?? '';
            $ways_type = $order->ways_type;
            $out_trade_no = $order->out_trade_no;
            $other_no = isset($data['other_no']) ? $data['other_no'] : "";
            $config = new CcbConfigController();

            $h_merchant = $config->ccb_merchant($store_id, $store_pid);
            if (!$h_merchant) {
                return json_encode([
                    'status' => '2',
                    'message' => '建行商户号不存在'
                ]);
            }
            if($ways_type==36001){
                $typeorder=2;
            }else if($ways_type==36002){
                $typeorder=3;
            }else if($ways_type==36003){
                $typeorder=4;
            }
            $obj = new \App\Api\Controllers\lianfu\PayController();
            $data1['MERCHANTID'] = $h_merchant->merchantid;//商户号
            $data1['POSID'] = $h_merchant->posid;//柜台号
            $data1['BRANCHID'] = $h_merchant->bankchid;//分行号
            $data1['TXCODE'] = 'PAY101';//交易码
            $data1['MERFLAG'] = 1;//商户类型
            $data1['ORDERID'] = $order->out_trade_no;//商户订单号
            $data1['QRYTIME'] = 1;//交易金额
            $data1['QRCODETYPE'] = $typeorder;//条码信息
            $return = $obj->order_query_ccb($data1,$h_merchant->pubkey);
            //支付成功
            if ($return['status'] == 1) {
                $pay_time = date('Y-m-d H:i:s', time());
                $trade_no = $return['data']['ORDERID'];
                $buyer_pay_amount = $order->total_amount;
                $buyer_pay_amount = number_format($buyer_pay_amount, 2, '.', '');
                $depBankSeq = $return['data']['ORDERID'];
                //改变数据库状态
                $table = isset($data['table']) ? $data['table'] : "";
                if ($table == "") {
                    $day = date('Ymd', time());
                    $table = 'orders_' . $day;
                }
                if ($order->pay_status != 1) {
                    $insert_data = [
                        'status' => '1',
                        'pay_status' => 1,
                        'pay_status_desc' => '支付成功',
                        'buyer_logon_id' => '',
                        'trade_no' => $trade_no,
                        'pay_time' => $pay_time,
                        'buyer_pay_amount' => $buyer_pay_amount,
                        'other_no' => $depBankSeq
                    ];
                    $this->update_day_order($insert_data, $out_trade_no, $table);

                    //支付成功后的动作
                    $data = [
                        'ways_type' => $order->ways_type,
                        'company' => $order->company,
                        'ways_type_desc' => $order->ways_type_desc,
                        'source_type' => '36001', //返佣来源
                        'source_desc' => '建行', //返佣来源说明
                        'total_amount' => $buyer_pay_amount,
                        'out_trade_no' => $order->out_trade_no,
                        'rate' => $order->rate,
                        'fee_amount' => $order->fee_amount,
                        'merchant_id' => $order->merchant_id,
                        'store_id' => $order->store_id,
                        'user_id' => $order->user_id,
                        'config_id' => $config_id,
                        'store_name' => $order->store_name,
                        'ways_source' => $order->ways_source,
                        'pay_time' => $pay_time,
                        'device_id' => isset($order->device_id) ? $order->device_id : "",
                    ];
                    PaySuccessAction::action($data);
                }

                return json_encode([
                    'status' => '1',
                    'pay_status' => 1,
                    'message' => '支付成功',
                    'data' => [
                        'ways_source' => $order->ways_source,
                        'pay_time' => $pay_time,
                        'out_trade_no' => $order->out_trade_no,
                        'pay_amount' => $buyer_pay_amount,
                        'total_amount' => $order->total_amount,
                        'other_no' => $other_no,
                        'trade_no' => $trade_no
                    ]
                ]);
            }
            elseif($return['status'] == 2) {
                return json_encode([
                    'status' => '1',
                    'pay_status' => '2', //1-成功 2-支付中
                    'message' => '用户支付中',
                    'data' => [
                        'out_trade_no' => $order->out_trade_no,
                        'pay_amount' => $order->pay_amount,
                        'total_amount' => $order->total_amount,
                        'ways_source'=>$order->ways_source
                    ]
                ]);
            }
            else {
                return json_encode([
                    'status' => '1',
                    'pay_status' => 3,
                    'message' => '支付失败',
                    'data' => [
                        'out_trade_no' => $order->out_trade_no,
                        'pay_amount' => $order->pay_amount,
                        'total_amount' => $order->total_amount,
                        'ways_source'=>$order->ways_source
                    ]
                ]);
            }

        } catch (\Exception $e) {
            return json_encode([
                'status' => -1,
                'message' => $e->getMessage().' | '.$e->getFile().' | '.$e->getLine()
            ]);
        }
    }


}
